﻿var MainMod = angular.module('MainMod', ['ngRoute']);

MainMod.controller('HeadCtrl', function ($scope) {
    $scope.title = "Head Title";
});

MainMod.controller('BodyCtrl', ['$scope', 'gridInfo', '$compile', '$location', function ($scope, gridInfo, $compile, $location) {
    $scope.title = "Demo Title";
    $scope.hideBtn = "Hide Grid";
    $scope.hideGrid = false;
    $scope.btnStyle = "btn btn-info";
    $scope.items = gridInfo.grid1Info();

    $scope.clickMe = function (item) {
        alert(item.name + " was clicked with a data value of " + item.data);
    };
    $scope.addDirective = function () {
        var placeholder = document.getElementById('placeholder');
        var ele = angular.element(placeholder);
        ele.append("<div dir-temp />");
        $compile(ele)($scope);
    };
    $scope.hidingGrid = function() {
        $scope.hideGrid = !$scope.hideGrid;
        $scope.hideBtn = $scope.hideGrid ? "Show Grid" : "Hide Grid";
        $scope.btnStyle = $scope.hideGrid ? "btn btn-success" : "btn btn-info";

    };
   
}]);

MainMod.controller('TabOneCtrl', function ($scope) {
    $scope.tab = 'This is Tab One';
    
});
MainMod.controller('TabTwoCtrl', ['$scope', 'gridInfo', function ($scope, gridInfo) {
    $scope.tab = 'This is Tab With Grid';
    $scope.items = gridInfo.grid2Info();
    $scope.currentPage = 0;
    $scope.pageSize = 5;
    $scope.numberOfPages = function() {
        return Math.ceil($scope.items.length / $scope.pageSize);
    };
    $scope.changePage = function (goTo) {
        $scope.numberOfPages();
        $scope.currentPage = $scope.currentPage + goTo;
    };
}]);
MainMod.controller('TabThreeCtrl', ['$scope', 'promiseService', '$q',
    function ($scope, promiseService, $q) {
        $scope.tab = 'This is Tab With Promises';

        function success(data) {
            var placeholder = document.getElementById("promiseholder");
            var ele = angular.element(placeholder);
            ele.append("<div>"+data+"</div>");
        }
        function error(data) {
            alert("there was an error");
        }
        promiseService.promisedItem('1').then(success, error);
        promiseService.promisedItem('2').then(success);
        promiseService.promisedItem('3').then(success);
    
        //multiple promises at once
        var multi = $q.all([promiseService.promisedItem('A'), promiseService.promisedItem('B'), promiseService.promisedItem('C')]);
        multi.then(success);
}]);

MainMod.directive('dirTemp', ['$log',
    function($log) {
        return {
            priority: 50,
            restrict: 'A',
            replace: false,
            transclude: true,
            scope: true,
            templateUrl: '/jasmine/src/directiveTemp.html',
            link: function (scope, element, attr) {
                scope.deleteDirective = function() {
                    var x = confirm("Do you want to Delete?");
                    if (x) {
                        element[0].parentElement.removeChild(element[0]);
                    }
                };
            }
        };
    }]);
MainMod.filter('startFrom', function () {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    };
});
MainMod.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/tab1', { templateUrl: 'View1.html', controller:'TabOneCtrl' });
    $routeProvider.when('/tab2', { templateUrl: 'View2.html', controller: 'TabTwoCtrl' });
    $routeProvider.when('/tab3', { templateUrl: 'View3.html', controller: 'TabThreeCtrl' });
    $routeProvider.otherwise({ template: 'Please choose a tab' });

}]);
    